// 专门用于封装购物车功能的组件

// createContext做一个二次封装
import React, { Component, createContext } from "react";
// const 的解构使用冒号取别名
// import 的解构使用as取别名
const { Provider, Consumer: CartConsumer } = createContext();

class CartProvider extends Component {
  state = {
    count: 10,
  };

  increment = () => {
    this.setState((prevState) => ({
      count: prevState.count + 1,
    }));
  };

  decrement = () => {
    this.setState((prevState) => ({
      count: prevState.count - 1,
    }));
  };

  render() {
    return (
      <Provider
        value={{
          count: this.state.count,
          increment: this.increment,
          decrement: this.decrement,
        }}
      >
        {this.props.children}
      </Provider>
    );
  }
}

export { CartProvider, CartConsumer };
